<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-13 19:58:49
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-07 18:11:34
 -->
<template>
  <div class="app-container">
    <eHeader :query="query" :sup_this="sup_this" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" row-key="id" size="small" style="width: 100%;">
      <!-- <el-table-column prop="id" label="ID" /> -->
      <el-table-column prop="wwx_dept_name" width="200px" label="部门名称" />
      <el-table-column prop="crop_name" label="所属企业" />
      <el-table-column prop="wwx_dept_id" label="部门ID" />
      <el-table-column prop="wwx_dept_order" label="部门排序" />
      <el-table-column prop="super_role.name" label="上级权限" />
      <el-table-column prop="ordinary_role.name" label="成员权限" />
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column prop="updated_at" label="修改时间" />
      <el-table-column label="操作" width="180px" align="center">
        <template slot-scope="scope">
          <edit v-if="checkPermission(['ADMIN'])" :data="scope.row" :sup_this="sup_this" :roles="roles" />
          <el-popconfirm v-permission="['ADMIN']" title="确定删除本条数据吗？" @onConfirm="subDelete(scope.row.id)">
            <el-button slot="reference" type="danger" size="mini">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'

export default {
  components: { eHeader, edit },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      sup_this: this,
      roles: []
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
    this._getRoles()
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = '/admin/v1/department'
      const sort = 'id,desc'
      this.params = { page: this.page, limit: this.limit, sort: sort }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    _getRoles() {
      this.$api.permission.role
        .getRoleTree({})
        .then(res => {
          this.roles = res.data
        })
    },
    subDelete(id) {
      this.delLoading = true
      this.$api.enterprise.department.del(id).then(res => {
        this.delLoading = false
        this.init()
        this.$notify({ title: '删除成功', type: 'success', duration: 2500 })
      }).catch(err => {
        this.delLoading = false
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
